---
sidebar_label: Groq
---

import CodeBlock from "@theme/CodeBlock";

# ChatGroq

## Setup

In order to use the Groq API you'll need an API key. You can sign up for a Groq account and create an API key [here](https://wow.groq.com/).

You'll first need to install the [`@langchain/groq`](https://www.npmjs.com/package/@langchain/groq) package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/groq
```

import UnifiedModelParamsTooltip from "@mdx_components/unified_model_params_tooltip.mdx";

<UnifiedModelParamsTooltip></UnifiedModelParamsTooltip>

## Usage

import ChatGroqExample from "@examples/models/chat/chat_groq.ts";

<CodeBlock language="typescript">{ChatGroqExample}</CodeBlock>

:::info
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/2ba59207-1383-4e42-b6a6-c1ddcfcd5710/r)
:::

## Tool calling

Groq chat models support calling multiple functions to get all required data to answer a question.
Here's an example:

import GroqTools from "@examples/models/chat/integration_groq_tool_calls.ts";

<CodeBlock language="typescript">{GroqTools}</CodeBlock>

### `.withStructuredOutput({ ... })`

:::info
The `.withStructuredOutput` method is in beta. It is actively being worked on, so the API may change.
:::

You can also use the `.withStructuredOutput({ ... })` method to coerce `ChatGroq` into returning a structured output.

The method allows for passing in either a Zod object, or a valid JSON schema (like what is returned from [`zodToJsonSchema`](https://www.npmjs.com/package/zod-to-json-schema)).

Using the method is simple. Just define your LLM and call `.withStructuredOutput({ ... })` on it, passing the desired schema.

Here is an example using a Zod schema and the `functionCalling` mode (default mode):

import WSAZodExample from "@examples/models/chat/integration_groq_wsa_zod.ts";

<CodeBlock language="typescript">{WSAZodExample}</CodeBlock>

## Streaming

Groq's API also supports streaming token responses. The example below demonstrates how to use this feature.

import ChatStreamGroqExample from "@examples/models/chat/chat_stream_groq.ts";

<CodeBlock language="typescript">{ChatStreamGroqExample}</CodeBlock>

:::info
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/72832eb5-b9ae-4ce0-baa2-c2e95eca61a7/r)
:::
